import loginView from "$v/login";

import {
    loginRequest
} from '../api/admin.js' //引入登录api

export default (req, res) => {
    res.render(loginView())

    //给登录按钮绑定点击事件
    document.loginForm.loginBtn.onclick = async function (e) {
        //表单提交会刷新页面,阻止浏览器默认行为
        e.preventDefault();

        //获取用户输入的内容
        const adminNameValue = document.loginForm.adminName.value.trim();
        const passwordValue = document.loginForm.passWord.value.trim();

        //发送登录请求
        try {
            const loginResult = await loginRequest({
                adminName: adminNameValue,
                password: passwordValue
            });


            //当登录成功以后 把token保存在本地存储中
            localStorage.setItem("user_token", loginResult.data.data.token)

            //跳转到adv界面
            router.go("/adv")

        } catch (e) {
            alert(e.message)
        }
    }
}